package com.ace.slide.client.app;

import com.ace.slide.client.listener.Listener;
import com.ace.slide.client.listener.ListenerEvent;
import com.ace.slide.client.service.SlideShowBuilderService;
import com.ace.slide.client.service.SlideShowBuilderServiceAsync;
import com.ace.slide.client.ui.LoginPanel;
import com.ace.slide.client.ui.ProfileTab;
import com.extjs.gxt.themes.client.Slate;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.ToolBarEvent;
import com.extjs.gxt.ui.client.state.CookieProvider;
import com.extjs.gxt.ui.client.state.StateManager;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.ThemeManager;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.toolbar.TextToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;

import ext.ux.theme.black.client.Black;
import ext.ux.theme.darkgray.client.DarkGray;
import ext.ux.theme.olive.client.Olive;
import ext.ux.theme.purple.client.Purple;
import ext.ux.theme.slickness.client.Slickness;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Ace implements EntryPoint {

	private StateManager stateManager;

	private Viewport mainViewport;

	private TabPanel centerTabPanel;

	private LoginPanel login;
	private ProfileTab profileTab;

	private HtmlContainer northPanel;

	private ContentPanel westPanel;
	private ToolBar westToolBar;
	TextToolItem editProfile;

	private Image welcomeImage;

	private ContentPanel welcomePanel;
	
	private SlideShowBuilderServiceAsync service;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		service = (SlideShowBuilderServiceAsync) GWT.create(SlideShowBuilderService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        String url = GWT.getModuleBaseURL() + "showBuilderService";
        endpoint.setServiceEntryPoint(url);
        
        Registry.register("builderService", service);
		
		ThemeManager.register(Slate.SLATE);
		ThemeManager.register(Black.BLACK);
		ThemeManager.register(DarkGray.DARKGRAY);
		ThemeManager.register(Olive.OLIVE);
		ThemeManager.register(Purple.PURPLE);
		ThemeManager.register(Slickness.SLICKNESS);

		stateManager = new StateManager();
		stateManager.setProvider(new CookieProvider(null, null, null, false));

		String name = stateManager.getString("name");
		// String sessionId = Cookies.getCookie("sid");

		mainViewport = new Viewport();
		mainViewport.setLayout(new BorderLayout());

		if (name != null) {
			handleLogin(true);
		} else {
			handleLogin(false);
		}

		RootPanel.get("AceApp").add(mainViewport);
	}

	private void createApp() {
		createNorth();
		createCenter();
		createWest();
		addListeners();
	}

	private void handleLogin(boolean isLoggedIn) {

		if (isLoggedIn) {
			if (welcomePanel != null) {
				welcomePanel.removeFromParent();
			}
			createApp();
			mainViewport.layout();
		} else {
			if (centerTabPanel != null) {
				centerTabPanel.removeFromParent();
			}
			if (westPanel != null) {
				westPanel.removeFromParent();
			}
			createNorth();
			createWelcome();
			mainViewport.layout();
		}
		Registry.register("mainViewport", mainViewport);
	}

	private void createNorth() {

		StringBuffer sb = new StringBuffer();
		sb
				.append("<div id='header' class='x-small-editor' style='background-color:black'><div id='theme' style='float:right;'></div><div id=title><img src='resources/aceText.png'/></div></div>");
		northPanel = new HtmlContainer(sb.toString());
		northPanel.setEnableState(false);

		login = new LoginPanel();
		login.addLoginListener(new LoginListener());
		northPanel.add(login, "#theme");

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 75);
		data.setMargins(new Margins(0, 0, 2, 0));

		mainViewport.add(northPanel, data);
		Registry.register("northPanel", northPanel);
	}

	private void createWelcome() {
		welcomeImage = new Image("resources/logo.png");
		welcomePanel = new ContentPanel();
		welcomePanel.setFrame(true);
		welcomePanel.setScrollMode(Scroll.AUTO);
		welcomePanel.add(welcomeImage);

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		data.setMargins(new Margins(0, 0, 2, 0));

		mainViewport.add(welcomePanel, data);
	}

	private void createWest() {

		westPanel = new ContentPanel();
		westPanel.setHeading("Navigation Bar");

		westToolBar = new ToolBar();
		editProfile = new TextToolItem("Profile");

		westToolBar.add(editProfile);
		westPanel.setTopComponent(westToolBar);

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST);
		data.setMargins(new Margins(0, 0, 0, 0));
		data.setCollapsible(true);
		data.setSplit(true);

		mainViewport.add(westPanel, data);
		Registry.register("westPanel", westPanel);
	}

	private void createCenter() {

		centerTabPanel = new TabPanel();
		centerTabPanel.setAnimScroll(true);
		centerTabPanel.setResizeTabs(true);
		centerTabPanel.setMinTabWidth(115);
		centerTabPanel.setTabScroll(true);

		Image img = new Image("resources/fire.png");

		TabItem mainTab = new TabItem("Main");
		mainTab.setScrollMode(Scroll.AUTO);
		mainTab.add(img);

		centerTabPanel.add(mainTab);
		mainViewport.add(centerTabPanel, new BorderLayoutData(
				LayoutRegion.CENTER));
		Registry.register("centerTabPanel", centerTabPanel);
	}

	private class LoginListener implements Listener {

		public void change(ListenerEvent event) {
			if (event.getProperty().equals("Login")) {
				handleLogin((Boolean) event.getNewValue());
			}
		}
	}

	private void addListeners() {
		editProfile.addSelectionListener(new SelectionListener<ToolBarEvent>() {
			@Override
			public void componentSelected(ToolBarEvent ce) {
				if (profileTab != null) {
					if (centerTabPanel.getItemByItemId("profileTab") == null)  {
						centerTabPanel.add(profileTab);
					}
					centerTabPanel.setSelection(profileTab);
				} else {
					profileTab = new ProfileTab();
					centerTabPanel.add(profileTab);
					centerTabPanel.setSelection(profileTab);
				}
			}
		});
	}
}
